<?php
	defined('IN_TS') or die('Access Denied.');
	
	$userid = aac('user')->isLogin();
	
	
	switch($ts){
		//基本信息
		case "base":
			$wishid = intval($_GET['wishid']);
			$strWish = $new['wish']->find('wish',array(
				'wishid'=>$wishid,
			));
			$strWish['content'] = tsDecode($strWish['content']);
			if ($strWish ['addtime'] < strtotime('-7 day') && $TS_USER ['isadmin'] == 0) {
				tsNotice('超过一周，不允许编辑，如需代为编辑请给微澜图书馆公众号后台留言');
			}
			$arrGroup = $new['wish']->findAll('group','isplace>0','groupid asc');
			if($strWish['userid']!=$userid && $GLOBALS['TS_USER']['isadmin']==0){
				tsNotice('非法操作！');
			}
			
			$title = '编辑心愿信息';
			include template("edit_base");
			break;
		
		case "basedo":

			$wishid = intval($_POST['wishid']);
            $strWish = $new['wish']->find('wish',array(
                'wishid'=>$wishid,
            ));
            if($strWish['userid']!=$userid && $GLOBALS['TS_USER']['isadmin']==0){
                tsNotice('非法操作！');
            }

			$title = trim($_POST['title']);
			$typeid = intval($_POST['typeid']);
			$limittime = trim($_POST['limittime']);
			$quota = intval($_POST['quota']);
			$link = trim($_POST['link']);
			$wisher = trim($_POST['wisher']);
			$contact = trim($_POST['contact']);
			$phone = trim($_POST['phone']);
			$grade = intval($_POST['grade']);
			$class = intval($_POST['class']);
			$groupid = intval($_POST['groupid']);
			$content = tsClean($_POST['content']);
			$photo = trim($_POST['photo']);

			if($photo==''){
				tsNotice('必须上传封面照片！');
			};
			$img_path = $photo;

			// $img_path = getTextPhotos(tsDecode($content));
			// if($img_path==''){
			// 	tsNotice('请至少插入一张图片作为封面！');
			// };
			// $count = count($img_path);
			// $img_path = $img_path[$count-1];
			//$url=preg_replace('#/$#','',SITE_URL);
			//$img_path = $url.$img_path;




			$limittime = trim(str_replace('AM','',$limittime));
			$limittime = trim(str_replace('PM','',$limittime));
			$limittime = strtotime($limittime);

			if($limittime==''){
				tsNotice('设一个期限呗');
			}

			if($quota==''){
				tsNotice('数目不能为空');
			}
			if($wisher==''){
				tsNotice('愿主不能为空');
			}
			if($groupid==''){
				tsNotice('分馆不能为空');
			}

			if($title == '' || $content == ''){
				tsNotice('标题和内容不能为空');
			}

			$arrGroup = aac('group')->getOneGroup($groupid);

			if($contact==''){
				$contact = $arrGroup['contact'];
		}

			if($phone==''){
				$phone = $arrGroup['phone'];
		}
			//更新数据
			$new['wish']->update('wish',array(
				'wishid'=>$wishid,
			),array(
				'title'	=> $title,
				'typeid' => $typeid,
				'limittime' => $limittime,
				'quota'	=> $quota,
				'link' => $link,
				'contact' => $contact,
				'phone' => $phone,
				'wisher' => $wisher,
				'groupid' => $groupid,
				'grade'  => $grade,
				'class'	=> $class,
				'content' => $content,
				// 'isaudit'=>$isaudit,
				'path'=> dirname($img_path),
				'photo'=> $img_path,
			));
			
			//上传
			// $arrUpload = tsUploadPhotoUrl($img_path,$wishid,'wish',array('jpg','gif','png'));
			// if($arrUpload){
			// 	$new['wish']->update('wish',array(
			// 		'wishid'=>$wishid,
			// 	),array(
			// 		'path'=>$arrUpload['path'],
			// 		'photo'=>$arrUpload['url'],
			// 	));
            //     //删除缓存图片
            //     tsDimg ( $arrUpload ['url'], 'wish', '300', '', $arrUpload ['path'] );
            //     tsDimg ( $arrUpload ['url'], 'wish', '500', '', $arrUpload ['path'] );
			// 	tsDimg ( $arrUpload ['url'], 'wish', '200', '', $arrUpload ['path'] );
			// 	tsDimg ( $arrUpload ['url'], 'wish', '100', '', $arrUpload ['path'] );
			// }
			
			header("Location: ".tsUrl('wish','show',array('id'=>$wishid)));
		
			break;
			
		case "join":
			
			$wishid = intval($_GET['wishid']);
			$strWish = $new['wish']->find('wish',array(
				'wishid'=>$wishid,
			));

            if($strWish['userid']!=$userid && $GLOBALS['TS_USER']['isadmin']==0){
                tsNotice('非法操作！');
            }
			
			$arrUser = $new['wish']->findAll('wish_users',array(
				'wishid'=>$wishid,
			),'addtime desc');

			foreach($arrUser as $k => $item){
				$strUser = aac('user')->find('user_info',array('userid'=>$item['userid']));
				$arrUser[$k]['username']=$strUser['username'];
				$arrUser[$k]['phone']=$strUser['phone'];
			}

			
			$title = '申领信息';
			include template('edit_join');
			
			break;

	}